package com.dgut.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.dgut.dao.IAccountDao;
import com.dgut.domain.Account;

import javax.sql.DataSource;
import java.sql.*;
import java.util.List;

public class AccountDaoImpl implements IAccountDao {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public List<Account> findAll() {
        System.out.println("findALL");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
//            DruidDataSource dataSource = new  DruidDataSource();
//            dataSource.setUrl("jdbc:mysql:///spring");
//            dataSource.setUsername("root");
////            dataSource.setPassword("xieman123");
//            connection = DriverManager.getConnection("jdbc:mysql:///spring", "root", "xieman123");
            connection = dataSource.getConnection();
            statement = connection.prepareStatement("select * from account");
            resultSet  = statement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Float money = resultSet.getFloat("money");
                System.out.println("id:"+id+" name:"+name+" moeny:"+money);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}
